package org.nightrunner.d3.controller.management;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.nightrunner.d3.constant.CRUDConstant;
import org.nightrunner.d3.entity.Category;
import org.nightrunner.d3.log.SysLog;
import org.nightrunner.d3.model.DefaultPaginationInfo;
import org.nightrunner.d3.model.IPaginationInfo;
import org.nightrunner.d3.model.PaginationModel;
import org.nightrunner.d3.service.ICategoryService;
import org.nightrunner.d3.service.IEquipmentService;
import org.nightrunner.d3.util.OperateInfo;
import org.nightrunner.d3.util.OperateInfo.Link;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.google.gson.JsonObject;

/**
 * Management Controller
 * 
 * @author NightRunner
 * @date 2012-7-26
 * @version 1.0
 */
@Controller
@RequestMapping("management/category/")
public class CategoryManagementController implements CRUDConstant {

	private static final String LINK_INDEX = "/management/category/index";

	private static final String LINK_NAME_INDEX = "装备分类首页";

	private static final String RETURN_VIEW_PREFIX = "management/category/";

	@Autowired
	public ICategoryService categoryService;

	@Autowired
	public IEquipmentService equipmentService;

	private static final Logger LOGGER = SysLog.CONTROLLER;

	@RequestMapping(value = "preadd/{categoryId}", method = RequestMethod.GET)
	public String preAdd(@PathVariable("categoryId") String categoryId,
			HttpServletResponse response, ModelMap modelMap) throws Exception {

		LOGGER.info(categoryId);

		Category superCategory = categoryService.getCategory(categoryId);

		modelMap.addAttribute("superCategory", superCategory);
		return RETURN_VIEW_PREFIX + VIEW_ADD;
	}

	@RequestMapping(value = "add/{categoryId}", method = RequestMethod.POST)
	public String add(@PathVariable("categoryId") String categoryId,
			HttpServletRequest request, ModelMap modelMap) throws Exception {
		String categoryName = request.getParameter("categoryName");
		String categoryDescription = request
				.getParameter("categoryDescription");
		String sort = request.getParameter("sort");

		Category superCategory = categoryService.getCategory(categoryId);

		Category matterCategory = new Category();

		matterCategory.setCategoryDescription(categoryDescription);
		matterCategory.setCategoryName(categoryName);
		matterCategory.setSuperCategory(superCategory);
		try {
			matterCategory.setSort(Integer.parseInt(sort));
		} catch (Exception ex) {
			matterCategory.setSort(0);
		}

		categoryService.addCategory(matterCategory);

		OperateInfo operateInfo = new OperateInfo();
		operateInfo.setInfo(OPERATE_INFO_UPDATE_OK);
		operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));

		modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		return VIEW_OPERATE_INFO;
	}

	@RequestMapping(value = "delete/{categoryId}", method = RequestMethod.GET)
	public String delete(@PathVariable("categoryId") String categoryId,
			HttpServletResponse response, ModelMap modelMap) throws Exception {

		categoryService.deleteCategory(categoryService.getCategory(categoryId));

		OperateInfo operateInfo = new OperateInfo();
		operateInfo.setInfo(OPERATE_INFO_DELETE_OK);
		operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));

		modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		return VIEW_OPERATE_INFO;
	}

	@RequestMapping(value = "preupdate/{categoryId}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable("categoryId") String categoryId,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
		Category category = categoryService.getCategory(categoryId);

		modelMap.addAttribute("category", category);
		return RETURN_VIEW_PREFIX + VIEW_UPDATE;
	}

	@RequestMapping(value = "update/{categoryId}", method = RequestMethod.POST)
	public String update(@PathVariable("categoryId") String categoryId,
			HttpServletRequest request, ModelMap modelMap) throws Exception {
		Category category = categoryService.getCategory(categoryId);

		String categoryName = request.getParameter("categoryName");
		String categoryDescription = request
				.getParameter("categoryDescription");
		String sort = request.getParameter("sort");

		category.setCategoryDescription(categoryDescription);
		category.setCategoryName(categoryName);
		try {
			category.setSort(Integer.parseInt(sort));
		} catch (Exception ex) {
			category.setSort(0);
		}

		categoryService.updateCategory(category);

		OperateInfo operateInfo = new OperateInfo();
		operateInfo.setInfo(OPERATE_INFO_UPDATE_OK);
		operateInfo.addLink(new Link(LINK_NAME_INDEX, LINK_INDEX));

		modelMap.addAttribute(REQUEST_KEY_OPERATE_INFO, operateInfo);

		return VIEW_OPERATE_INFO;
	}

	@RequestMapping(value = "index", method = RequestMethod.GET)
	public ModelAndView index(HttpServletResponse response, ModelMap modelMap)
			throws Exception {
		return new ModelAndView(new RedirectView("view/"
				+ ICategoryService.CATEGORY_ROOT_ID), modelMap);
	}

	@RequestMapping(value = "", method = RequestMethod.GET)
	public ModelAndView root(HttpServletResponse response, ModelMap modelMap)
			throws Exception {
		return new ModelAndView(new RedirectView("view"), modelMap);
	}

	@RequestMapping(value = "view/", method = RequestMethod.GET)
	public ModelAndView viewRoot(
			HttpServletResponse response, ModelMap modelMap) throws Exception {
		return new ModelAndView(new RedirectView("view/"
				+ ICategoryService.CATEGORY_ROOT_ID), modelMap);
	}

	@RequestMapping(value = "view", method = RequestMethod.GET)
	public ModelAndView viewRoot2(
			HttpServletResponse response, ModelMap modelMap) throws Exception {
		return new ModelAndView(new RedirectView("view/"
				+ ICategoryService.CATEGORY_ROOT_ID), modelMap);
	}

	@RequestMapping(value = "view/{categoryId}", method = RequestMethod.GET)
	public String view(@PathVariable("categoryId") String categoryId,
			HttpServletResponse response, ModelMap modelMap) throws Exception {
		Category category = categoryService.getCategory(categoryId);

		modelMap.addAttribute("category", category);

		return RETURN_VIEW_PREFIX + VIEW_VIEW;
	}

	@RequestMapping(value = "list-{currentPage}-{rowSize}", method = RequestMethod.GET)
	public String list(ModelMap modelMap,
			@PathVariable("rowSize") Integer rowSize,
			@PathVariable("currentPage") Integer currentPage) {

		IPaginationInfo pagination = new DefaultPaginationInfo();
		pagination.setCurrentPage(currentPage);
		pagination.setRowSize(rowSize);

		PaginationModel<List<Category>> paginationModel = categoryService
				.getCategory(pagination);
		modelMap.addAttribute("paginationModel", paginationModel);

		return RETURN_VIEW_PREFIX + VIEW_LIST;
	}

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public ModelAndView list(ModelMap modelMap) {
		return new ModelAndView(new RedirectView("list-0-10"), modelMap);
	}

	@RequestMapping(value = "ajaxchangecategorysort/{categoryId}_{sort}", method = RequestMethod.POST)
	@ResponseBody
	public String ajaxChangeSort(@PathVariable("categoryId") String categoryId,
			@PathVariable("sort") Integer sort, HttpServletResponse response,
			ModelMap modelMap) throws Exception {
		Category matterCategory = categoryService.getCategory(categoryId);

		matterCategory.setSort(sort);

		categoryService.updateCategory(matterCategory);

		JsonObject object = new JsonObject();

		object.addProperty(CRUD_SUCCESS, true);
		object.addProperty(CRUD_INFO, "修改成功");
		return object.toString();
	}
}
